package com.bolt.admin.web.controller;

import com.bolt.admin.common.base.BaseController;
import com.bolt.admin.module.quartz.dto.JobLogDTO;
import com.bolt.admin.module.quartz.dto.converter.JobLogConverter;
import com.bolt.admin.module.quartz.entity.JobLogEntity;
import com.bolt.admin.module.quartz.service.JobLogService;
import com.bolt.convention.data.PageInfo;
import com.bolt.convention.data.ResultMessage;
import com.bolt.convention.data.Returns;
import com.bolt.core.view.components.Form;
import com.bolt.support.spring.jpa.specification.QueryHelp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Auto Generate Javadoc
 *
 * @author 日期:2020-10-12 09:50:16
 *         定时任务日志
 **/
@Controller
@RequestMapping("/admin/quartz/log")
public class JobLogController extends BaseController <JobLogEntity, Long> {

    @Autowired
    private JobLogService logService;


    /**
     * 模块首页
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView index(Model model, HttpServletRequest request) {
        return super.index("/admin/quartz/log/index", request, model.asMap());
    }

    /**
     * 定时任务日志列表分页
     *
     * @return
     */
    @RequestMapping(value = "page", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage list(Pageable pageable, JobLogDTO dto) {
        PageInfo <JobLogEntity> pageInfo = logService.findPage(pageable, (root, criteriaQuery, criteriaBuilder)
                -> QueryHelp.getPredicate(root, dto, criteriaBuilder));
        return Returns.success(pageInfo.map(K -> JobLogConverter.toDTO(K)));

    }

    /**
     * 定时任务日志新增操作
     *
     * @return
     */
    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String create() {
        return "/admin/quartz/log/input-index";
    }

    /**
     * 定时任务日志编辑页
     *
     * @param id    菜单ID
     * @param model
     * @return
     */
    @RequestMapping(value = "edit", method = RequestMethod.GET)
    public String edit(@RequestParam("id") Long id, Model model) {
        JobLogEntity logEntity = logService.findOne(id).get();
        model.addAttribute(Form.PAGECONTEXT_FORMVALUES, JobLogConverter.toDTO(logEntity));
        return "/admin/quartz/log/input-index";
    }



}